import 'package:flutter/material.dart';

//点击事件统一封装
class MyGestureDetector extends StatefulWidget {
  final GestureTapCallback? onTap;
  final GestureLongPressCallback? onLongPress;
  final Widget? child;
  final HitTestBehavior? behavior;

  const MyGestureDetector({super.key, this.onTap, this.child, this.onLongPress, this.behavior});

  @override
  State<MyGestureDetector> createState() => _MyGestureDetectorState();
}

class _MyGestureDetectorState extends State<MyGestureDetector> {
  DateTime? _lastTapTime;

  void _handleTap() {
    final now = DateTime.now();
    if (_lastTapTime == null || now.difference(_lastTapTime!) > const Duration(milliseconds: 800)) {
      // 在这里处理点击事件
      _lastTapTime = now;
      widget.onTap?.call();
    } else {
      print('Double tap detected, ignoring');
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: widget.behavior, onLongPress: widget.onLongPress, onTap: _handleTap, child: widget.child);
  }
}
